import {
  machineVerifyCheckCode,
  machineVerifyGetCode
} from '@/components/machine-verification/hooks/request'
// 人机校验需要传入的appId,以及 getCode返回的token
const appId = 5
let token = ''
/*******************人机校验相关逻辑集合，便于多项目迁移使用*******************/
//获取验证码
export async function getCode() {
  const res = await machineVerifyGetCode(appId)
  if (res.code !== 0) throw new Error(res.message)
  token = res.data.token
  return res.data
}

// 校验验证码正确性
export async function checkCode(pointJson: string) {
  try {
    const res = await machineVerifyCheckCode(pointJson, appId, token)
    if (res.code !== 0) throw new Error(res.message)
    return res.data.captchaVerification
  } catch (e) {
    return
  }
}

// 将字符串组成图片base64
export function joinToImgBase64(url?: string) {
  if (!url) return ''
  return `data:image/png;base64,${url}`
}
